<?php
class Manage extends MY_Controller
{
	public function __construct()
	{
		log_message('debug', '开始调用GameArea->Manage->__construct接口');
		parent::__construct();
		$this->loadLanguageFile('application/controllers/GameArea/');
		log_message('debug', '调用GameArea->Manage->__construct接口结束');
	}
	
	
	public function showView()
	{
		$this->load->view('module/GameArea/manage.htm');
	}
	
	
	public function getListData()
	{
	    log_message('debug', '开始调用GameArea->Manage->getListData接口=====');
		$this->verifyPrivilege();
		
		try {
			$checkPrivilege = $this->param->getParam('checkPrivilege', array('method' => 'int', 'min' => 0, 'max' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			$page = $this->param->getParam('page', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			$rows = $this->param->getParam('rows', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			
			$sql = "
				select
					<selectColumns>
				from
					`t_data_game_area` a
				left join
					`t_data_operator` b
				on
					a.`operatorId` = b.`operatorId`
				left join
					`t_data_server` c
				on
					a.`serverId` = c.`serverId`
				left join
					`t_data_game_version` d
				on
					a.`version` = d.`id`
				left join
					`t_data_battle_server` f
				on
					a.`battleServerId` = f.`battleServerId`
				left join
					`t_data_operator` g
				on
					f.`operatorId` = g.`operatorId`
			";
			
			if($checkPrivilege === '1') {
				$sql .= "
					left join
						`t_data_user_operator_privilege` e
					on
						a.`operatorId` = e.`operatorId`
						and
						e.`userId` = {$this->currUserId}
					where
						e.`privilegeValue` = 1
                    order by
					   b.`operatorId`,
					   a.`gameAreaId`
				";
			}
			else {
				$sql .= "
					where
						1 = 1
					order by
					   b.`operatorId`,
					   a.`gameAreaId`
				";
			}
			$getTotalSql = str_replace('<selectColumns>', "count(*)", $sql);
			$this->returnData['data']['total'] = $this->gmDatabase->getColumn($getTotalSql);
			$getDataSql = str_replace('<selectColumns>', "a.`gameAreaId`, a.`operatorId`, b.`operatorName`, a.`areaNum`, a.`httpPort`, if(a.`serverId` = 0, '', a.`serverId`) as `serverId`, c.`serverName`, if(a.`portGroup` = 0, '', a.`portGroup`) as `portGroup`, a.`domain`, a.`gameDir`, a.`databaseName`, a.`status`, a.`openDatetime`, a.`mergeDatetime`, a.`mergeDestAreaNum`, a.`closeDatetime`, concat(b.`operatorName`, '-S', a.`areaNum`) as gameAreaName, d.`id` as `versionId`, d.`version`, a.`lineNum`, a.`battleServerId`, concat(g.`operatorName`, '-', f.`areaNum`) as `battleServerName`", $sql);
			
			if($page && $rows) {
				$startRow = ($page - 1) * $rows;
				$getDataSql .= " limit {$startRow}, {$rows}";
			}
			//error_log("getDataSql = " . $getDataSql);
			$this->returnData['data']['rows'] = $this->gmDatabase->getArray($getDataSql);
		}
		catch(MY_Param_Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		catch(MY_Mysql_Exception $e) {
			$this->setErrorCode(1);
		}
		
		catch(Exception $e) {
			$this->setErrorCode($e->getCode());
		}

		$this->returnJson();
		log_message('debug', '=====调用GameArea->Manage->getListData接口结束=====');
	}
	
	
	public function add()
	{
		log_message('debug', '=====开始调用GameArea->add接口=====');
		$this->verifyPrivilege();
		
		try {
			$operatorId = $this->param->getParam('operatorId', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 5001, 'invalidErrorCode' => 1));
			$areaNum = $this->param->getParam('areaNum', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 5002, 'invalidErrorCode' => 5003));
			$serverId = $this->param->getParam('serverId', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 5004, 'invalidErrorCode' => 1));
			$portGroup = $this->param->getParam('portGroup', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 5005, 'invalidErrorCode' => 5006));
			$domain = $this->param->getParam('domain', array('method' => 'string', 'emptyErrorCode' => 5007, 'invalidErrorCode' => 5008));
			$httpPort = $this->param->getParam('httpPort', array('method' => 'string', 'emptyErrorCode' => 10001, 'invalidErrorCode' => 10002));
			$gameDir = $this->param->getParam('gameDir', array('method' => 'string', 'emptyErrorCode' => 5009, 'invalidErrorCode' => 5010));
			$version = $this->param->getParam('version', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 5025, 'invalidErrorCode' => 1));
			$database = $this->param->getParam('databaseName', array('method' => 'string', 'emptyErrorCode' => 5011, 'invalidErrorCode' => 5012));
			$openDatetime = $this->param->getParam('openDatetime', array('method' => 'string', 'emptyErrorCode' => 5013, 'invalidErrorCode' => 1));
			$lineNum = $this->param->getParam('lineNum', array('method' => 'int', 'min' => 1, 'max' => 4, 'emptyErrorCode' => 5026, 'invalidErrorCode' => 5027));
			$battleServerId = $this->param->getParam('battleServerId', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			
			$this->vaildPortGroup($serverId, $portGroup);
			$sql = "
				insert into
					`t_data_game_area`
				set
					`operatorId` = '{$operatorId}',
					`areaNum` = '{$areaNum}',
					`serverId` = '{$serverId}',
					`portGroup` = '{$portGroup}',
					`domain` = '{$domain}',
                    `httpPort` = '{$httpPort}',
					`gameDir` = '{$gameDir}',
					`databaseName` = '{$database}',
					`version` = '{$version}',
					`lineNum` = '{$lineNum}',
					`battleServerId` = '{$battleServerId}',
					`status` = '1',
					`openDatetime` = '{$openDatetime}'
			";
			$this->gmDatabase->query($sql);
		}
		
		catch(MY_Param_Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		catch(MY_Mysql_Exception $e) {
			$errorCode = $e->getCode();
		
			if($errorCode === 1062) {
				$this->setErrorCode(5014);
			}
			else {
				$this->setErrorCode(1);
			}
		}
		
		catch(Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		$this->returnJson();
		log_message('debug', '=====调用GameArea->add接口结束=====');
	}
	
	
	public function modify() {
		log_message('debug', '=====开始调用GameArea->modify接口=====');
		$this->verifyPrivilege();
		
		try {
			$gameAreaId = $this->param->getParam('gameAreaId', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1));
			$operatorId = $this->param->getParam('operatorId', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 5001, 'invalidErrorCode' => 1));
			$areaNum = $this->param->getParam('areaNum', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 5002, 'invalidErrorCode' => 5003));
			$serverId = $this->param->getParam('serverId', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 5004, 'invalidErrorCode' => 1), 'post', true);
			$portGroup = $this->param->getParam('portGroup', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 5005, 'invalidErrorCode' => 5006), 'post', true);
			$domain = $this->param->getParam('domain', array('method' => 'string', 'emptyErrorCode' => 5007, 'invalidErrorCode' => 5008));
			$httpPort = $this->param->getParam('httpPort', array('method' => 'string', 'emptyErrorCode' => 10001, 'invalidErrorCode' => 10002));
			$gameDir = $this->param->getParam('gameDir', array('method' => 'string', 'emptyErrorCode' => 5009, 'invalidErrorCode' => 5010));
			$databaseName = $this->param->getParam('databaseName', array('method' => 'string', 'emptyErrorCode' => 5011, 'invalidErrorCode' => 5012));
			$status = $this->param->getParam('status', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 5015, 'invalidErrorCode' => 1));
			$version = $this->param->getParam('version', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			$lineNum = $this->param->getParam('lineNum', array('method' => 'int', 'min' => 1, 'max' => 4, 'emptyErrorCode' => 5026, 'invalidErrorCode' => 5027));
			$battleServerId = $this->param->getParam('battleServerId', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			$openDatetime = $this->param->getParam('openDatetime', array('method' => 'string', 'emptyErrorCode' => 5013, 'invalidErrorCode' => 1));

			$this->vaildPortGroup($serverId, $portGroup, $gameAreaId);
			$sql = "
				update
					`t_data_game_area`
				set
					`operatorId` = '{$operatorId}',
					`areaNum` = '{$areaNum}',
					`serverId` = '{$serverId}',
					`portGroup` = '{$portGroup}',
					`domain` = '{$domain}',
                    `httpPort` = '{$httpPort}',
					`gameDir` = '{$gameDir}',
					`databaseName` = '{$databaseName}',
					`version` = '{$version}',
					`lineNum` = '{$lineNum}',
					`battleServerId` = '{$battleServerId}',
					`status` = '{$status}',
					`openDatetime` = '{$openDatetime}'
			";
			
			$sql .= "
				where
					`gameAreaId` = '{$gameAreaId}'
			";
			$this->gmDatabase->query($sql);
		}
		
		catch(MY_Param_Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		catch(MY_Mysql_Exception $e) {
			$errorCode = $e->getCode();
		
			if($errorCode === 1062) {
				$this->setErrorCode(5014);
			}
			else {
				$this->setErrorCode(1);
			}
		}
		
		catch(Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		$this->returnJson();
		log_message('debug', '=====调用GameArea->modify接口结束=====');
	}
	
	public function install() {
		log_message('debug', '=====开始调用GameArea->Manage->install=====');
		$this->verifyPrivilege();
		
		try {
			$gameAreaId = $this->param->getParam('gameAreaId', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1));
			
			$sql = "
				select
					`status`
				from
					`t_data_game_area`
				where
					`gameAreaId` = '{$gameAreaId}'
			";
			
			$status = $this->gmDatabase->getColumn($sql);
			
			if($status === false) {
				throw new Exception('', 1);
			}
			
			if($status != 1) {
				throw new Exception('', 5024);
			}
			
			$sql = "
				select
					count(*)
				from
					`t_data_game_install_queue`
				where
					`gameAreaId` = '{$gameAreaId}'
					and
					`status` <= 2
			";
			
			if($this->gmDatabase->getColumn($sql) > 0) {
				throw new Exception('', 5024);
			}
			
			$sql = "
				insert into
					`t_data_game_install_queue`
				set
					`gameAreaId` = '{$gameAreaId}',
					`operateUserId` = '{$this->currUserId}',
					`operateDatetime` = now(),
					`status` = 1
			";
			$this->gmDatabase->query($sql);
		}
		
		catch(MY_Param_Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		catch(MY_Mysql_Exception $e) {
			$this->setErrorCode(1);
		}
		
		catch(Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		$this->returnJson();
		log_message('debug', '=====调用GameArea->Manage->install结束=====');
	}
	
	public function clearData() {
		log_message('debug', '=====开始调用GameArea->Manage->clearData=====');
		$this->verifyPrivilege();
		
		try {
			$gameAreaId = $this->param->getParam('gameAreaId', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1));
				
			$sql = "
				select
					`status`
				from
					`t_data_game_area`
				where
					`gameAreaId` = '{$gameAreaId}'
			";
				
			$status = $this->gmDatabase->getColumn($sql);
				
			if($status === false) {
				throw new Exception('', 1);
			}
				
			if( $status != 2 && $status != 3 ) {
				throw new Exception('', 5028);
			}
			
			$sql = "
				select
					count(*)
				from
					`t_data_game_clearData_queue`
				where
					`gameAreaId` = '{$gameAreaId}'
				and
					`status` <= 2
			";
				
			if($this->gmDatabase->getColumn($sql) > 0) {
				throw new Exception('', 5029);
			}
			
			$sql = "
				insert into
					`t_data_game_clearData_queue`
				set
					`gameAreaId` = '{$gameAreaId}',
					`operateUserId` = '{$this->currUserId}',
					`operateDatetime` = now(),
					`status` = 1
			";
			$this->gmDatabase->query($sql);
		}
		
		catch(MY_Param_Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		catch(MY_Mysql_Exception $e) {
			$this->setErrorCode(1);
		}
		
		catch(Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		
		$this->returnJson();
		log_message('debug', '=====调用GameArea->Manage->clearData结束=====');
	}
	
	private function vaildPortGroup($serverId, $portGroup, $gameAreaId = 0) {
		$sql = "
			select
				`maxGameAreaNum`
			from
				`t_data_server`
			where
				`serverId` = '{$serverId}'
		";
		$maxGameAreaNum = $this->gmDatabase->getColumn($sql);
	
		if($maxGameAreaNum === false) {
			throw new Exception('', 1);
		}
		
		$sql = "
			select
				count(*)
			from
				`t_data_server_portgroup`
			where
				`serverId` = '{$serverId}'
				and
				`status` = 2
		";
		$disablePortGroupNum = $this->gmDatabase->getColumn($sql);
	
		$sql = "
			select
				count(*)
			from
				`t_data_game_area`
			where
				`status` <= 5
				and
				`serverId` = '{$serverId}'
				and
				`gameAreaId` <> '{$gameAreaId}'
		";
		$currentGameAreaNum = $this->gmDatabase->getColumn($sql);
	
		log_message('debug', "maxGameAreaNum: {$maxGameAreaNum}");
		log_message('debug', "disablePortGroupNum: {$disablePortGroupNum}");
		log_message('debug', "currentAreaNum: {$currentGameAreaNum}");
	
		if($currentGameAreaNum >= $maxGameAreaNum - $disablePortGroupNum) {
			throw new Exception('', 5021);
		}

		
		if($portGroup > $maxGameAreaNum) {
			throw new Exception('', 5023);
		}
			
		$sql = "
			select
				count(*)
			from
				`t_data_server_portgroup`
			where
				`serverId` = '{$serverId}'
				and
				`portGroup` = '{$portGroup}'
				and
				`status` = 2
		";
			
		if($this->gmDatabase->getColumn($sql) > 0) {
			throw new Exception('', 5023);
		}
		
		$sql = "
			select
				count(*)
			from
				`t_data_game_area`
			where
				`status` <= 5
				and
					`serverId` = '{$serverId}'
				and
				`portGroup` = '{$portGroup}'
				and
				`gameAreaId` <> '{$gameAreaId}'
		";
		
		if($this->gmDatabase->getColumn($sql) > 0) {
			throw new Exception('', 5022);
		}		
	}
}